<!DOCTYPE html>
<html><head>
<title>Java - The Learning Experience 3</title>




<meta charset="utf-8">
<meta name="X-UA-Compatible" content="IE=edge">
<meta name="google-site-verification" content="">
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
<meta content="telephone=no" name="format-detection">
<meta name="description" content="">
<meta name="renderer" content="webkit">
<meta name="theme-color" content="#ffffff">



<meta property="og:title" content="Java - The Learning Experience 3" />
<meta property="og:description" content="Interfaces 接口 In its most common form, an interface is a group of related methods with empty bodies. A bicycle&rsquo;s behavior, if specified as an interface, might appear as follows：
interface BicycleInterface { // constant declarations, if any// method signatures void changeCadence(int newValue); void changeGear(int newValue); void speedUp(int increment); void applyBrakes(int decrement); } Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler." />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://wondrouswisdomcard.gitee.io/posts/java3/" />
<meta property="article:published_time" content="2020-10-14T00:00:00+00:00" />
<meta property="article:modified_time" content="2020-10-14T00:00:00+00:00" /><meta property="og:site_name" content="My Blog" />





<meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="Java - The Learning Experience 3"/>
<meta name="twitter:description" content="Interfaces 接口 In its most common form, an interface is a group of related methods with empty bodies. A bicycle&rsquo;s behavior, if specified as an interface, might appear as follows：
interface BicycleInterface { // constant declarations, if any// method signatures void changeCadence(int newValue); void changeGear(int newValue); void speedUp(int increment); void applyBrakes(int decrement); } Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler."/>







<script src="/vendor/js/jquery.min.js" ></script>
<script src="/vendor/js/popper.min.js" ></script>
<script src="/vendor/js/bootstrap.min.js" ></script>
<script src="/vendor/js/smooth-scroll.polyfills.min.js" ></script>
<link type="text/css" rel="stylesheet" href="/vendor/css/bootstrap.min.css">
<script src="/vendor/js/vue.min.js" ></script>






<link rel="stylesheet" href="https://wondrouswisdomcard.gitee.io/scss/journal.min.c116bc90d171283f099f173854157ec8f183f9073b93443b2c8ad82899ee9025.css" integrity="sha256-wRa8kNFxKD8Jnxc4VBV&#43;yPGD&#43;Qc7k0Q7LIrYKJnukCU=" media="screen">



<link rel="stylesheet" href="https://wondrouswisdomcard.gitee.io/scss/dark-mode.min.552aae4638a84aa57cf0b195750a49ea9131d3bb621d1ed3ebc9b14b18166536.css" integrity="sha256-VSquRjioSqV88LGVdQpJ6pEx07tiHR7T68mxSxgWZTY=" media="screen">


<script src="https://wondrouswisdomcard.gitee.io/js/loadCSS.js"></script>
<script>
  loadCSS("https://fonts.googleapis.com/css?family=Lora|Montserrat|Fira+Mono|Noto+Serif+SC|Material+Icons");
</script>




  
    <script src="https://wondrouswisdomcard.gitee.io/js/toc-collapse.js"></script>
  



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">
<script src="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>
<script src="/vendor/js/md5.min.js"></script>
<script>
  var gitalk = new Gitalk({
  clientID: 'your client id',
  clientSecret: 'your client secret',
  repo: 'repo name',
  owner: 'user',
  admin: ['user'],
  id: md5(location.pathname),
  distractionFreeMode: 'false'
  });
  window.onload = function () {
        gitalk.render('gitalk-container')
  }
</script>







<script src="https://cdn.jsdelivr.net/npm/vue-disqus@3/dist/vue-disqus.js"></script>


</head>
<body>
    	<div id="app"><div ref="sideContainer" class="side-container">
    
    <a class="a-block nav-head false" href="https://wondrouswisdomcard.gitee.io">
    
        <div class="nav-title">
            Wondrous WindomCard&#39;s Blog
        </div>
        
        <div class="nav-subtitle">
            Themed by Diary.
        </div>
        
    </a>

    <div class="nav-link-list">
        
        
            
            
            
                
            
            
            
            <a class="a-block nav-link-item active" href="/posts">
                Archive
            </a>
            
        
            
            
            
            
            
            <a class="a-block nav-link-item false" href="/categories">
                Categories
            </a>
            
        
            
            
            
            
            
            <a class="a-block nav-link-item false" href="/tags">
                Tags
            </a>
            
        
            
            
            
            
            
            <a class="a-block nav-link-item false" href="/index.xml">
                RSS Feed
            </a>
            
        
    </div>

    

    <div class="nav-footer">
        
Hugo Theme <a href="https://github.com/amazingrise/hugo-theme-diary">Diary</a> by <a href="https://amazingrise.net">Rise</a>
<br>
Ported from <a href="https://mak1t0.cc/" target="_blank" rel="noreferrer noopener">Makito</a>'s <a href="https://github.com/SumiMakito/hexo-theme-journal/" target="_blank" rel="noreferrer noopener">Journal.</a> <br>
<br>

&copy;
	
	This is a customized copyright.
	

    </div>
    
</div><div ref="extraContainer" class="extra-container">
    
    
    <div class="toc animated-visibility" :class="{ invisible: scrollY <= 140 }">


	<div class="toc-content">
	
		
		
		
		<center>- CATALOG -</center>
		
		
		<ul>
			
				
				
					
						
						
						
						
						
							<li>
								<a href="#interfaces-%e6%8e%a5%e5%8f%a3" onclick="onNavClick(`#interfaces-接口-nav`)" id="interfaces-接口-nav">
									Interfaces 接口
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
							
								<ul class="collapse" data-toggle="collapse">
							
								<ul class="collapse" data-toggle="collapse">
							
								<ul class="collapse" data-toggle="collapse">
							
						
						
							<li>
								<a href="#%e5%a4%9a%e6%8e%a5%e5%8f%a3%e7%9a%84%e7%bb%a7%e6%89%bf%e5%ae%9e%e4%be%8b%e6%b3%a8%e6%84%8f%e8%af%ad%e6%b3%95" onclick="onNavClick(`#多接口的继承实例注意语法-nav`)" id="多接口的继承实例注意语法-nav">
									多接口的继承实例：//注意语法
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
						
							<li>
								<a href="#using-an-interface-as-a-type" onclick="onNavClick(`#using-an-interface-as-a-type-nav`)" id="using-an-interface-as-a-type-nav">
									Using an Interface as a Type
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
						
							<li>
								<a href="#rewriting-interfaces" onclick="onNavClick(`#rewriting-interfaces-nav`)" id="rewriting-interfaces-nav">
									Rewriting Interfaces
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
							
								</ul>
							
						
						
						
							<li>
								<a href="#%e5%af%b9%e6%af%94%e6%8a%bd%e8%b1%a1%e7%b1%bb%e5%92%8c%e6%8e%a5%e5%8f%a3" onclick="onNavClick(`#对比抽象类和接口-nav`)" id="对比抽象类和接口-nav">
									对比抽象类和接口
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
							
								</ul>
							
								</ul>
							
						
						
						
							<li>
								<a href="#design-pattern-%e8%ae%be%e8%ae%a1%e6%a8%a1%e5%bc%8f" onclick="onNavClick(`#design-pattern-设计模式-nav`)" id="design-pattern-设计模式-nav">
									Design Pattern 设计模式
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
							
								<ul class="collapse" data-toggle="collapse">
							
								<ul class="collapse" data-toggle="collapse">
							
						
						
							<li>
								<a href="#gof-pattern" onclick="onNavClick(`#gof-pattern-nav`)" id="gof-pattern-nav">
									GoF Pattern
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
						
							<li>
								<a href="#pattern-singleton" onclick="onNavClick(`#pattern-singleton-nav`)" id="pattern-singleton-nav">
									Pattern: Singleton
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
						
							<li>
								<a href="#observer-pattern" onclick="onNavClick(`#observer-pattern-nav`)" id="observer-pattern-nav">
									Observer pattern
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
						
							<li>
								<a href="#decorator-pattern" onclick="onNavClick(`#decorator-pattern-nav`)" id="decorator-pattern-nav">
									Decorator Pattern
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
						
							<li>
								<a href="#proxy-pattern-%e4%bb%a3%e7%90%86%e6%a8%a1%e5%bc%8f" onclick="onNavClick(`#proxy-pattern-代理模式-nav`)" id="proxy-pattern-代理模式-nav">
									Proxy Pattern 代理模式
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
							
								</ul>
							
								</ul>
							
						
						
						
							<li>
								<a href="#basic-io" onclick="onNavClick(`#basic-io-nav`)" id="basic-io-nav">
									Basic I/O
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
							
								<ul class="collapse" data-toggle="collapse">
							
								<ul class="collapse" data-toggle="collapse">
							
						
						
							<li>
								<a href="#byte-streams-%e8%af%bb%e5%8f%96%e5%86%99%e5%85%a5%e6%96%87%e4%bb%b6%e5%ae%9e%e4%be%8b" onclick="onNavClick(`#byte-streams-读取写入文件实例-nav`)" id="byte-streams-读取写入文件实例-nav">
									Byte Streams 读取写入文件实例：
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
						
							<li>
								<a href="#character-streams-%e8%af%bb%e5%8f%96%e5%86%99%e5%85%a5%e6%96%87%e4%bb%b6%e5%ae%9e%e4%be%8b" onclick="onNavClick(`#character-streams-读取写入文件实例-nav`)" id="character-streams-读取写入文件实例-nav">
									Character Streams 读取写入文件实例：
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
						
							<li>
								<a href="#character-streams-buffuered%e8%af%bb%e5%8f%96%e5%86%99%e5%85%a5%e6%96%87%e4%bb%b6%e5%ae%9e%e4%be%8b" onclick="onNavClick(`#character-streams-buffuered读取写入文件实例-nav`)" id="character-streams-buffuered读取写入文件实例-nav">
									Character Streams (Buffuered)读取写入文件实例：
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
						
							<li>
								<a href="#filter-streams" onclick="onNavClick(`#filter-streams-nav`)" id="filter-streams-nav">
									Filter Streams
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
						
							<li>
								<a href="#scanner-and-formatting" onclick="onNavClick(`#scanner-and-formatting-nav`)" id="scanner-and-formatting-nav">
									Scanner and Formatting
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
							
								<ul class="collapse" data-toggle="collapse">
							
						
						
							<li>
								<a href="#systemoutformat" onclick="onNavClick(`#systemoutformat-nav`)" id="systemoutformat-nav">
									System.out.format();
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
							
								</ul>
							
						
						
						
							<li>
								<a href="#data-streams" onclick="onNavClick(`#data-streams-nav`)" id="data-streams-nav">
									Data Streams
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
						
							<li>
								<a href="#object-streams-%e5%af%b9%e8%b1%a1%e6%b5%81-object-serialization-%e5%af%b9%e8%b1%a1%e5%ba%8f%e5%88%97%e5%8c%96" onclick="onNavClick(`#object-streams-对象流-object-serialization-对象序列化-nav`)" id="object-streams-对象流-object-serialization-对象序列化-nav">
									Object Streams 对象流 （Object Serialization 对象序列化）
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
						
							<li>
								<a href="#file-io" onclick="onNavClick(`#file-io-nav`)" id="file-io-nav">
									File I/O
								</a>
							</li>
						
						
					
				
			
		</ul>
	</div>

</div>
    
    <div class="pagination">
        <a id="globalBackToTop" class="pagination-action animated-visibility" href="#top" :class="{ invisible: scrollY == 0 }">
            <i class="material-icons pagination-action-icon">
                keyboard_arrow_up
            </i>
        </a>
        
        <a class="pagination-action" v-on:click="toggleDarkMode">
            <i class="material-icons pagination-action-icon" v-if="isDarkMode">
                brightness_4
            </i>
            <i class="material-icons pagination-action-icon" v-else="isDarkMode">
                brightness_7
            </i>
        </a>
        
        
    </div>
</div>
<div class="single-column-drawer-container" ref="drawer"
     v-bind:class="{ 'single-column-drawer-container-active': isDrawerOpen }">
    <div class="drawer-content">
        <div class="drawer-menu">
            
            
            
                
                
                
                    
                
                
                
                <a class="a-block drawer-menu-item active" href="/posts">
                    Archive
                </a>
                
            
                
                
                
                
                
                <a class="a-block drawer-menu-item false" href="/categories">
                    Categories
                </a>
                
            
                
                
                
                
                
                <a class="a-block drawer-menu-item false" href="/tags">
                    Tags
                </a>
                
            
                
                
                
                
                
                <a class="a-block drawer-menu-item false" href="/index.xml">
                    RSS Feed
                </a>
                
            
            
            <div class="toc">


	<div class="toc-content">
	
		
		
		
		<center>- CATALOG -</center>
		
		
		<ul>
			
				
				
					
						
						
						
						
						
							<li>
								<a href="#interfaces-%e6%8e%a5%e5%8f%a3" onclick="onNavClick(`#interfaces-接口-nav`)" id="interfaces-接口-nav">
									Interfaces 接口
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
							
								<ul class="collapse" data-toggle="collapse">
							
								<ul class="collapse" data-toggle="collapse">
							
								<ul class="collapse" data-toggle="collapse">
							
						
						
							<li>
								<a href="#%e5%a4%9a%e6%8e%a5%e5%8f%a3%e7%9a%84%e7%bb%a7%e6%89%bf%e5%ae%9e%e4%be%8b%e6%b3%a8%e6%84%8f%e8%af%ad%e6%b3%95" onclick="onNavClick(`#多接口的继承实例注意语法-nav`)" id="多接口的继承实例注意语法-nav">
									多接口的继承实例：//注意语法
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
						
							<li>
								<a href="#using-an-interface-as-a-type" onclick="onNavClick(`#using-an-interface-as-a-type-nav`)" id="using-an-interface-as-a-type-nav">
									Using an Interface as a Type
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
						
							<li>
								<a href="#rewriting-interfaces" onclick="onNavClick(`#rewriting-interfaces-nav`)" id="rewriting-interfaces-nav">
									Rewriting Interfaces
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
							
								</ul>
							
						
						
						
							<li>
								<a href="#%e5%af%b9%e6%af%94%e6%8a%bd%e8%b1%a1%e7%b1%bb%e5%92%8c%e6%8e%a5%e5%8f%a3" onclick="onNavClick(`#对比抽象类和接口-nav`)" id="对比抽象类和接口-nav">
									对比抽象类和接口
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
							
								</ul>
							
								</ul>
							
						
						
						
							<li>
								<a href="#design-pattern-%e8%ae%be%e8%ae%a1%e6%a8%a1%e5%bc%8f" onclick="onNavClick(`#design-pattern-设计模式-nav`)" id="design-pattern-设计模式-nav">
									Design Pattern 设计模式
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
							
								<ul class="collapse" data-toggle="collapse">
							
								<ul class="collapse" data-toggle="collapse">
							
						
						
							<li>
								<a href="#gof-pattern" onclick="onNavClick(`#gof-pattern-nav`)" id="gof-pattern-nav">
									GoF Pattern
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
						
							<li>
								<a href="#pattern-singleton" onclick="onNavClick(`#pattern-singleton-nav`)" id="pattern-singleton-nav">
									Pattern: Singleton
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
						
							<li>
								<a href="#observer-pattern" onclick="onNavClick(`#observer-pattern-nav`)" id="observer-pattern-nav">
									Observer pattern
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
						
							<li>
								<a href="#decorator-pattern" onclick="onNavClick(`#decorator-pattern-nav`)" id="decorator-pattern-nav">
									Decorator Pattern
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
						
							<li>
								<a href="#proxy-pattern-%e4%bb%a3%e7%90%86%e6%a8%a1%e5%bc%8f" onclick="onNavClick(`#proxy-pattern-代理模式-nav`)" id="proxy-pattern-代理模式-nav">
									Proxy Pattern 代理模式
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
							
								</ul>
							
								</ul>
							
						
						
						
							<li>
								<a href="#basic-io" onclick="onNavClick(`#basic-io-nav`)" id="basic-io-nav">
									Basic I/O
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
							
								<ul class="collapse" data-toggle="collapse">
							
								<ul class="collapse" data-toggle="collapse">
							
						
						
							<li>
								<a href="#byte-streams-%e8%af%bb%e5%8f%96%e5%86%99%e5%85%a5%e6%96%87%e4%bb%b6%e5%ae%9e%e4%be%8b" onclick="onNavClick(`#byte-streams-读取写入文件实例-nav`)" id="byte-streams-读取写入文件实例-nav">
									Byte Streams 读取写入文件实例：
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
						
							<li>
								<a href="#character-streams-%e8%af%bb%e5%8f%96%e5%86%99%e5%85%a5%e6%96%87%e4%bb%b6%e5%ae%9e%e4%be%8b" onclick="onNavClick(`#character-streams-读取写入文件实例-nav`)" id="character-streams-读取写入文件实例-nav">
									Character Streams 读取写入文件实例：
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
						
							<li>
								<a href="#character-streams-buffuered%e8%af%bb%e5%8f%96%e5%86%99%e5%85%a5%e6%96%87%e4%bb%b6%e5%ae%9e%e4%be%8b" onclick="onNavClick(`#character-streams-buffuered读取写入文件实例-nav`)" id="character-streams-buffuered读取写入文件实例-nav">
									Character Streams (Buffuered)读取写入文件实例：
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
						
							<li>
								<a href="#filter-streams" onclick="onNavClick(`#filter-streams-nav`)" id="filter-streams-nav">
									Filter Streams
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
						
							<li>
								<a href="#scanner-and-formatting" onclick="onNavClick(`#scanner-and-formatting-nav`)" id="scanner-and-formatting-nav">
									Scanner and Formatting
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
							
								<ul class="collapse" data-toggle="collapse">
							
						
						
							<li>
								<a href="#systemoutformat" onclick="onNavClick(`#systemoutformat-nav`)" id="systemoutformat-nav">
									System.out.format();
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
							
								</ul>
							
						
						
						
							<li>
								<a href="#data-streams" onclick="onNavClick(`#data-streams-nav`)" id="data-streams-nav">
									Data Streams
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
						
							<li>
								<a href="#object-streams-%e5%af%b9%e8%b1%a1%e6%b5%81-object-serialization-%e5%af%b9%e8%b1%a1%e5%ba%8f%e5%88%97%e5%8c%96" onclick="onNavClick(`#object-streams-对象流-object-serialization-对象序列化-nav`)" id="object-streams-对象流-object-serialization-对象序列化-nav">
									Object Streams 对象流 （Object Serialization 对象序列化）
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
						
							<li>
								<a href="#file-io" onclick="onNavClick(`#file-io-nav`)" id="file-io-nav">
									File I/O
								</a>
							</li>
						
						
					
				
			
		</ul>
	</div>

</div>
            
        </div>
    </div>
</div>
<transition name="fade">
    <div v-bind:class="{ 'single-column-drawer-mask': mounted }" v-if="isDrawerOpen" v-on:click="toggleDrawer"></div>
</transition>
<nav ref="navBar" class="navbar sticky-top navbar-light single-column-nav-container">
    <div ref="navBackground" class="nav-background"></div>
    <div class="container container-narrow nav-content">
        <button id="nav_dropdown_btn" class="nav-dropdown-toggle" type="button" v-on:click="toggleDrawer">
            <i class="material-icons">
                menu
            </i>
        </button>
        <a ref="navTitle" class="navbar-brand" href="https://wondrouswisdomcard.gitee.io">
            Wondrous WindomCard&#39;s Blog
        </a>
        
        <button type="button" class="nav-darkmode-toggle" v-on:click="toggleDarkMode">
            <i class="material-icons" v-if="isDarkMode">
                brightness_4
            </i>
            <i class="material-icons" v-else="isDarkMode">
                brightness_7
            </i>
        </button>
        
    </div>
</nav>
<div class="single-column-header-container" ref="pageHead"
     v-bind:style="{ transform: 'translateZ(0px) translateY('+.3*scrollY+'px)', opacity: 1-navOpacity }">
    <a href="https://wondrouswisdomcard.gitee.io">
        <div class="single-column-header-title">Wondrous WindomCard&#39;s Blog</div>
        
        <div class="single-column-header-subtitle">Themed by Diary.</div>
        

    </a>
</div>

            <div id="content">
<div ref="streamContainer" class="stream-container">
    <div class="post-list-container post-list-container-shadow">
        <div class="post">
            
            
            

            <div class="post-head-wrapper-text-only"
                
            >
                <div class="post-title">
                    Java - The Learning Experience 3
                    
                    <div class="post-meta">
                        
                        <time itemprop="datePublished">
                            2020-10-14 00:00
                        </time>
                        

                        
                            <i class="material-icons" style="">folder</i>
                                <a href="/categories/">[JavaNote]</a>
                                &nbsp;
                        

                        
                            <i class="material-icons" style="">label</i>
                            
                                <a href="/tags/java">Java</a>
                                &nbsp;
                            
                                <a href="/tags/interfaces">Interfaces</a>
                                &nbsp;
                            
                                <a href="/tags/design-pattern">Design Pattern</a>
                                &nbsp;
                            
                                <a href="/tags/basic-i/o">Basic I/O</a>
                                &nbsp;
                            
                        
                        
                    </div>
                </div>
            </div>
            
            <div class="post-body-wrapper">
                
                <div class="post-body" v-pre>
                
                    <h1 id="interfaces-接口">Interfaces 接口</h1>
<p>In its most common form, an interface is <strong>a group of related methods with empty bodies</strong>. A bicycle&rsquo;s behavior, if specified as an interface, might appear as follows：</p>
<pre><code>interface BicycleInterface { 
    // constant declarations, if any

    // method signatures 
    void changeCadence(int newValue); 
    void changeGear(int newValue); 
    void speedUp(int increment); 
    void applyBrakes(int decrement); 
}
</code></pre>
<p>Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler.
（不同牌子的汽车，有相同的操作，但这些不同场子的车不一定有同一个父类）</p>
<p>术语：<strong>类去实现一个接口</strong>，保留字<strong>implements</strong></p>
<p>Interfaces cannot be instantiated——they can only be implemented by classes or <strong>extended by other interfaces</strong>.</p>
<p>Java does not permit multiple inheritance but interfaces provide an alternative. In Java, a class can inherit from only one class but it can implement more than one interface.</p>
<p>An interface name can be used anywhere a type can be used.</p>
<h4 id="多接口的继承实例注意语法">多接口的继承实例：//注意语法</h4>
<p>An interface declaration consists of modifiers, the keyword interface, the interface name, a comma-separated list of parent interfaces (if any), and the interface body.</p>
<pre><code>public interface GroupedInterface extends Interface1, Interface2, Interface3 {
    // constant declarations 
    double E = 2.718282; 
    // base of natural logarithms
    // method signatures 
    void doSomething (int i, double x); 
    int doSomethingElse(String s);
</code></pre>
<p>}</p>
<h4 id="using-an-interface-as-a-type">Using an Interface as a Type</h4>
<pre><code>InterfaceAinterfaceName = new ClassB(); 
</code></pre>
<h4 id="rewriting-interfaces">Rewriting Interfaces</h4>
<pre><code>public interface DoIt { 
    void doSomething(int i, double x); 
    int doSomethingElse(String s); 
}
//you want to add a third method to DoIt

//问题操作
public interface DoIt { 
    void doSomething(int i, double x); 
    int doSomethingElse(String s); 
    boolean didItWork(int i, double x, String s);
}// If you make this change, all classes that implement the old DoIt interface will break because they don't implement the interface anymore. 

//Now users of your code can choose to continue to use the old interface or to upgrade to the new interface
public interface DoItPlus extends DoIt {
    boolean didItWork(int i, double x, String s);
}
</code></pre>
<h3 id="对比抽象类和接口">对比抽象类和接口</h3>
<ol>
<li>
<p>abstract classes can contain fields that are not static and final, and they can contain implemented methods.</p>
</li>
<li>
<p>It is possible to define a class that does not implement all of the interface methods, provided that the class is declared to be abstract.</p>
</li>
</ol>
<hr>
<h1 id="design-pattern-设计模式">Design Pattern 设计模式</h1>
<h3 id="gof-pattern">GoF Pattern</h3>
<h5 id="creational-pattern">Creational Pattern:</h5>
<p>Factory Method, Abstract Factory, <strong>Singleton</strong>, Builder, Prototype</p>
<h5 id="structural-pattern">Structural Pattern:</h5>
<p>Adapter, Bridge, Composite, <strong>Decorator</strong>, Facade, Flyweight, Proxy</p>
<h5 id="behavioral-pattern">Behavioral Pattern:</h5>
<p>Command, Interpreter, Iterator, Mediator, <strong>Observer</strong>, State, Strategy, Chain of Responsibility, Visitor, Template Method</p>
<h3 id="pattern-singleton">Pattern: Singleton</h3>
<p>定义：A pattern in which a class has only one instance and the class can create that instance itself</p>
<p>特点：</p>
<ol>
<li>在类里定义了一个该类的静态实例</li>
<li>构造函数为privated</li>
<li>成员函数getInstance()返回类的那个静态实例（如果instance == null，则new.</li>
</ol>
<p>例子：</p>
<p><img src="../../images/java3/3.png" alt="3"></p>
<h3 id="observer-pattern">Observer pattern</h3>
<p>定义：In Observer pattern, the class can inform other objects which subscribe to a change of its state. Any object which is interested in this change, can subscribe to the target object, and receive this information while it occurs.</p>
<p>类的设计：The Pattern contains two roles: <strong>Subject</strong>: a class that can inform others while its state has change. <strong>Observer</strong>: a class that interested in the change of the subject.</p>
<p><em>Subject</em> : an abstract class, which has <strong>add</strong> method, <strong>remove</strong> method, a <strong>List, whose element type is Observer</strong> a abstract method named <strong>notifyObserver</strong>. 
<em>ConcreteSubject</em>: a class inherite from the Subject class, and implement the abstract method notifyObserver.</p>
<p><em>Observer</em> : an <em>interface or abstract class</em>, which has a <strong>notify</strong> method. 
<em>ConcreteObserver</em> : a class inherite from the Subject class, and implement the abstract method notify.</p>
<p>例子：</p>
<pre><code>import java.util.*;
public class RMBrateTest {
    public static void main(String[] args) {
        Rate rate = new RMBrate();
        Company watcher1 = new ImportCompany();
        Company watcher2 = new ExportCompany();
        rate.add(watcher1);
        rate.add(watcher2);
        rate.change(10);
        rate.change(-9);
    }
}

// abstract: Rate
abstract class Rate {
    protected List&lt;Company&gt; companys = new ArrayList&lt;Company&gt;();

    // add
    public void add(Company company) {
        companys.add(company);
    }

    // remove
    public void remove(Company company) {
        companys.remove(company);
    }

    public abstract void change(int number);
}

class RMBrate extends Rate {
    public void change(int number) {
        for (Company obs : companys) {
            ((Company) obs).response(number);
        }
    }

}

// abstrct observer: Company
interface Company {
    void response(int number);
}

class ImportCompany implements Company {
    public void response(int number) {
        if (number &gt; 0) {
            System.out.println(&quot;RMBrate increase&quot; + number + &quot;, improved the profit margin of import companies&quot;);
        } 
        else if (number &lt; 0) {
            System.out.println(&quot;RMBrate decrease&quot; + (-number) + &quot;, reduced the profit margin of import companies&quot;);
        } } }
</code></pre>
<h3 id="decorator-pattern">Decorator Pattern</h3>
<p>定义： the Decorator Pattern is a design pattern that allows behavior to be added to an individual object, without affecting the behavior of other objects from the same class.</p>
<p>we just need to implement the interface of the extended (decorated) object (Component) transparently by forwarding all requests to it.</p>
<p>每个装饰者类继承装饰者类，且都有一个被装饰者的对象作为数据成员，并且重写装饰者类的成员函数（如果需要的话）</p>
<p>例子：</p>
<pre><code>public class StarBuzzCoffeeDemo {

    public static void main(String arg[]) {

        Coffee coffer = new SimpleCoffee();
        coffer = new WithMilk(coffer);
        System.out.println(coffer.getCost() + &quot;$  &quot; + coffer.getIngredients());
        coffer = new WithSprinkles(coffer);
        System.out.println(coffer.getCost() + &quot;$  &quot; + coffer.getIngredients());
    }
}

interface Coffee {
    public double getCost();

    public String getIngredients();
}

class SimpleCoffee implements Coffee {
    @Override
    public double getCost() {
        return 1;
    }

    @Override
    public String getIngredients() {
        return &quot;Coffee&quot;;
    }
}

class BlackCoffee implements Coffee {
    @Override
    public double getCost() {
        return 2;
    }

    @Override
    public String getIngredients() {
        return &quot;BlackCoffee&quot;;
    }
}

abstract class CoffeeDecorator  implements Coffee {
    private final Coffee decoratedCoffee;

    public CoffeeDecorator (Coffee c) {
        this.decoratedCoffee = c;
    }

    @Override
    public double getCost() {
        return decoratedCoffee.getCost();
    }

    @Override
    public String getIngredients() {
        return decoratedCoffee.getIngredients();
    }
}

class WithMilk extends CoffeeDecorator  {
    public WithMilk(Coffee c) {
        super(c);
    }

    @Override
    public String getIngredients() {
        return super.getIngredients() + &quot;, Milk&quot;;
    }
}

class WithSprinkles extends CoffeeDecorator  {
    public WithSprinkles(Coffee c) {
        super(c);
    }

    @Override
    public double getCost() {
        return super.getCost() + 0.2;
    }

    @Override
    public String getIngredients() {
        return super.getIngredients() + &quot;, Sprinkles&quot;;
    }
}
</code></pre>
<h3 id="proxy-pattern-代理模式">Proxy Pattern 代理模式</h3>
<p>定义：In Proxy Pattern, the class can provide a sub-interface to another class, which provide other object with a proxy to control access to that object.</p>
<hr>
<h1 id="basic-io">Basic I/O</h1>
<p>流支持不同类型的数据：包括简单的字节，内置数据类型，对象</p>
<p>Java.io package 里有两部分：字符流(16-bit UTF-16 character stream)和字节流(8 bits byte streams),I/O既可以是基于文本(data-based)的，也可以是基于二进制数据(binary data-based)的:</p>
<p>输入输出流(Inout streams or output streams)是字节流(byte stream)，而Readers or writers 是字符流(character stream)的</p>
<h3 id="byte-streams-读取写入文件实例">Byte Streams 读取写入文件实例：</h3>
<pre><code>import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyBytes{
    public static void main(String[] args) throws IOException{
        FileInputStream in = null;
        FileOutoutStream out = null;
        try{
            in = new FileInputStream(&quot;test_in.txt&quot;);
            out = new FileOutputStream(&quot;test_out.txt&quot;);
            int c;
            while((c = in.read()) != 1)//read()从in中读取一个字节 a byte of data
            {
                out.write(c);
            }
        } finally{
            if(in != null){
                in.close();
            }
            if(out != null){
                out.close();
            }
        }
    }
}

//finally作为异常处理的一部分，它只能用在try/catch语句中，并且附带一个语句块
//finally表示这段语句最终一定会被执行（不管有没有抛出异常），经常被用在需要释放资源的情况下。
</code></pre>
<h3 id="character-streams-读取写入文件实例">Character Streams 读取写入文件实例：</h3>
<pre><code>import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CopyCharacter{
    public static void main(String[] args) throws IOException{
        FileReader in = null;
        FileWriter out = null;
        try{
            in = new FileReader(&quot;test_in.txt&quot;);
            out = new FileWriter(&quot;test_out.txt&quot;);
            int c;
            while((c = in.read()) != 1)//read()从in中读取一个字符 a single character
            {
                out.write(c);
            }
        } finally{
            if(in != null){
                in.close();
            }
            if(out != null){
                out.close();
            }
        }
    }
}
</code></pre>
<h3 id="character-streams-buffuered读取写入文件实例">Character Streams (Buffuered)读取写入文件实例：</h3>
<pre><code>import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.IOException;
public class CopyLines{
    public static void main(String[] args) throws IOException{
        BufferedReader in = null;
        PrintWriter out = null;
        try{
            in = new BufferedReader(&quot;test_in.txt&quot;);
            out = new PrintWriter(&quot;test_out.txt&quot;);
            String i;
            while((i = in.readLine()) != 1)//readLline()从in中读取一行字符串
            {
                out.write(c);
            }
        } finally{
            if(in != null){
                in.close();
            }
            if(out != null){
                out.close();
            }
        }
    }
}
</code></pre>
<h3 id="filter-streams">Filter Streams</h3>
<p>它是一个抽象类 我们要去继承它，然后定义成员函数 public int read()</p>
<h3 id="scanner-and-formatting">Scanner and Formatting</h3>
<pre><code>import java.io.*;
import java.util.Scanner;
public class ScanXan{
    public static void main(String[] args)throws IOException{
        double sum = 0.0;
        Scanner s = null;
        try{
            s = new Scanner(new BufferedReader(new FileReader(&quot;text_in.txt&quot;)));
            while(s.hasNext())
            {
                System.out.println(s.next());
            }
            while(s.hasNext())
            {
                if(s.hasNextDouble) //Formatting 
                {
                    sum += s.nextDouble();
                }
                else
                {
                    s.next();
                }
            }
        } finally{
            if(s != null){
                s.close();
            }
        }
        System.out.println(sum);
    }
}
</code></pre>
<h4 id="systemoutformat">System.out.format();</h4>
<p><img src="../../images/java3/1.png" alt="1"></p>
<h3 id="data-streams">Data Streams</h3>
<p>支持内置数据类型和String，有专门的Read/Write methods  比如Char有readChar()和writeChar() 
案例：
<img src="../../images/java3/2.png" alt="2"></p>
<h3 id="object-streams-对象流-object-serialization-对象序列化">Object Streams 对象流 （Object Serialization 对象序列化）</h3>
<h3 id="file-io">File I/O</h3>
<hr>

                    
                    <HR width="100%" id="EOF">
		    <p style="color:#777;">Last modified on 2020-10-14</p>
                    
                </div>
            </div>
            
            
            <nav class="post-pagination">

                
                <a class="newer-posts" href="https://wondrouswisdomcard.gitee.io/posts/homework7/">
			Next<br>Homework of Week 7
                </a>
                
                
                
                <a class="older-posts" href="https://wondrouswisdomcard.gitee.io/posts/homework6/">
			Previous<br>Homework of Week 6
                </a>
                
            </nav>
            <div class="post-comment-wrapper">
                
<div class="comments">
    <vue-disqus shortname="Your disqus short name."></vue-disqus>
</div>
<noscript><noscript>Please activate JavaScript to view comments.</noscript></noscript>
<a href="https://disqus.com/" class="dsq-brlink">Comments powered by <span class="logo-disqus">Disqus</span></a>



<div id="gitalk-container"></div>






            </div>
        </div>
    </div>
</div>

            </div><div id="single-column-footer">
Hugo Theme <a href="https://github.com/amazingrise/hugo-theme-diary">Diary</a> by <a href="https://amazingrise.net">Rise</a>
<br>
Ported from <a href="https://mak1t0.cc/" target="_blank" rel="noreferrer noopener">Makito</a>'s <a href="https://github.com/SumiMakito/hexo-theme-journal/" target="_blank" rel="noreferrer noopener">Journal.</a> <br>
<br>

&copy;
	
	This is a customized copyright.
	
</div>
            </div>
    <script>
let app;

app = new Vue({
    el: '#app',
    data: {
        scrollY: 0,
        navOpacity: 0,
        isDrawerOpen: false,
        mounted: false,
        isDarkMode: false
    },
    methods: {
            sgn(t, x) {
                let k = 1. / (1. - 2 * t);
                if (x <= t) return 0;
                else if (x >= 1 - t) return 1;
                else {
                    return k * (x - t);
                }
            },
            handleScroll() {
                this.scrollY = window.scrollY;
                this.navOpacity = this.sgn(.0, Math.min(1, Math.max(0, window.scrollY / (this.pageHeadHeight() - this.navBarHeight() * 0.8))));
                const {navBar, navBackground, navTitle, extraContainer, streamContainer} = this.$refs;

                if (this.navOpacity >= 1) {
                    navBackground.style.opacity = 1;
                    navTitle.style.opacity = 1;
                } else {
                    navBackground.style.opacity = 0;
                    navTitle.style.opacity = 0;
                }
            },
            handleResize() {
                const {navBar, navBackground, navTitle, extraContainer, streamContainer} = this.$refs;
                extraContainer.style.left = (streamContainer.offsetWidth - extraContainer.offsetWidth) + 'px';
            },
            navBarHeight() {
                return this.$refs.navBar.offsetHeight;
            },
            pageHeadHeight() {
                return this.$refs.pageHead.offsetHeight;
            },
            toggleDrawer() {
                this.isDrawerOpen = !this.isDrawerOpen;
                document.getElementsByTagName('html')[0].style.overflow = this.isDrawerOpen ? 'hidden' : 'unset';
            },
            closeDrawer() {
                this.isDrawerOpen = false;
                document.getElementsByTagName('html')[0].style.overflow = this.isDrawerOpen ? 'hidden' : 'unset';
            },
            toggleDarkMode() {
                this.isDarkMode = !this.isDarkMode;
                if (this.isDarkMode==true){
                    document.cookie = "night=1;path=/";
                    document.body.classList.add("night");
                } else {
                    document.cookie = "night=0;path=/";
                    document.body.classList.remove("night");
                }
            }
    },
    created() {
        window.addEventListener('scroll', this.handleScroll);
        window.addEventListener('resize', this.handleResize);
        window._nonDesktop = function () {
            let check = false;
            (function (a) {
                if (/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino|android|ipad|playbook|silk/i.test(a) || /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(a.substr(0, 4))) check = true;
            })(navigator.userAgent || navigator.vendor || window.opera);
            return check;
        };
        
        var night = document.cookie.replace(/(?:(?:^|.*;\s*)night\s*\=\s*([^;]*).*$)|^.*$/, "$1");
        if (night==""){
            if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
                
            }
        }else{
            
            if (night=="1") {
                this.toggleDarkMode();
            }
        }
    },
    mounted() {
        this.handleScroll();
        this.handleResize();
        this.mounted = true;

        
    },
    destroyed() {
        window.removeEventListener('scroll', this.handleScroll);
        window.removeEventListener('resize', this.handleResize);
    }
});
</script>

<script src="https://wondrouswisdomcard.gitee.io/js/journal.js"></script>
    </body>
</html>
